﻿<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:loc="clr-namespace:LockCrypt"
    xmlns:conv="clr-namespace:LockCrypt.Converters"
    xmlns:Core="clr-namespace:LockCrypt.Core;assembly=LockCrypt.Core"
    xmlns:dg="clr-namespace:Microsoft.Windows.Controls;assembly=WpfToolkit"
    xmlns:lex="http://schemas.root-project.org/xaml/presentation"
    xmlns:l49ui="clr-namespace:Lime49.UI;assembly=Lime49">
    <HierarchicalDataTemplate DataType="{x:Type Core:IGroup}" ItemsSource="{Binding Children}" x:Key="TreeViewGroupTemplate">
        <HierarchicalDataTemplate.Resources>
            <conv:IconConverter x:Key="IconConverter"/>
            <conv:DebugConverter x:Key="dbg"/>
        </HierarchicalDataTemplate.Resources>
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
            <Image x:Name="imgIcon" Source="{Binding Icon,Converter={StaticResource IconConverter}, ConverterParameter=16}" Stretch="Uniform" Width="16" Height="16" VerticalAlignment="Center"/>
            <TextBlock Text="{Binding Name}" TextWrapping="Wrap" TextTrimming="CharacterEllipsis"/>
        </StackPanel>
        <HierarchicalDataTemplate.Triggers>
            <MultiDataTrigger>
                <MultiDataTrigger.Conditions>
                    <Condition Binding="{Binding Icon}" Value="{x:Static Core:Constants.DefaultGroupIcon}"/>
                    <Condition Binding="{Binding IsExpanded,RelativeSource={RelativeSource TemplatedParent}}" Value="True"/>
                    <Condition Binding="{Binding HasItems,RelativeSource={RelativeSource TemplatedParent}}" Value="True"/>
                </MultiDataTrigger.Conditions>
                <MultiDataTrigger.Setters>
                    <Setter TargetName="imgIcon" Property="Source" Value="pack://application:,,,/Resources/Images/folder_open_16.png"/>
                </MultiDataTrigger.Setters>
            </MultiDataTrigger>
        </HierarchicalDataTemplate.Triggers>
    </HierarchicalDataTemplate>
    <HierarchicalDataTemplate DataType="{x:Type loc:Account}" x:Key="TreeViewAccountTemplate">
        <HierarchicalDataTemplate.Resources>
            <conv:IconConverter x:Key="IconConverter"/>
        </HierarchicalDataTemplate.Resources>
        <StackPanel Orientation="Horizontal">
            <Image x:Name="imgIcon" Source="{Binding Icon,Converter={StaticResource IconConverter}, ConverterParameter=16}"  Stretch="Uniform" Width="16" Height="16" VerticalAlignment="Center"/>
            <TextBlock Text="{Binding Name}" TextWrapping="Wrap" TextTrimming="CharacterEllipsis"/>
        </StackPanel>
    </HierarchicalDataTemplate>

    <DataTemplate x:Key="AccountTemplate" DataType="{x:Type Core:IAccount}">
        <DataTemplate.Resources>
            <BitmapImage x:Key="copy_16" UriSource="pack://application:,,,/Resources/Images/copy_16.png" />
            <BitmapImage x:Key="key_16" UriSource="pack://application:,,,/Resources/Images/key_gold_16.png" />
            <BitmapImage x:Key="phone_16" UriSource="pack://application:,,,/Resources/Images/phone_16.png" />
            <BitmapImage x:Key="web_16" UriSource="pack://application:,,,/Resources/Images/web_16.png" />
            <conv:MetaFlagsToVisibilityConverter x:Key="MetaFlagsToVisibilityConverter"/>
            <conv:MetaFlagsToBoolConverter x:Key="MetaFlagsToBoolConverter"/>
            <conv:ObfuscatedFieldConverter x:Key="ObfuscatedFieldConverter"/>
            <BooleanToVisibilityConverter x:Key="BoolToVisibilityConverter"/>
        </DataTemplate.Resources>
        <Border BorderThickness="1" Background="White" BorderBrush="{StaticResource btnPressStroke2}" CornerRadius="2">
            <ScrollViewer HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>
                    <ListBox x:Name="itemContainer" Grid.Row="0" ItemsSource="{Binding Fields}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Disabled" Background="Transparent" BorderThickness="0">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="2*"/>
                                        <ColumnDefinition Width="3*"/>
                                        <ColumnDefinition Width="84"/>
                                    </Grid.ColumnDefinitions>
                                    <TextBlock Grid.Column="0" Text="{Binding Name}" TextWrapping="Wrap" TextTrimming="CharacterEllipsis" TextAlignment="Right" FontWeight="Bold" Margin="3,0,3,0" />
                                    <TextBlock x:Name="txtValue" Grid.Column="1" TextWrapping="Wrap" TextTrimming="CharacterEllipsis" Margin="0,1,3,1"  Tag="{Binding Meta,Converter={StaticResource MetaFlagsToBoolConverter},ConverterParameter=Password}">
                                        <TextBlock.Text>
                                            <MultiBinding Converter="{StaticResource ObfuscatedFieldConverter}">
                                                <Binding Path="Value"/>
                                                <Binding Path="Meta"/>
                                                <Binding Path="Tag" RelativeSource="{RelativeSource Self}" />
                                            </MultiBinding>
                                        </TextBlock.Text>
                                    </TextBlock>
                                    <StackPanel Grid.Column="2" Orientation="Horizontal" HorizontalAlignment="Right">
                                        <l49ui:ImageButton Image="{StaticResource key_16}" Style="{StaticResource LiteImageButton}" ToolTip="{lex:LocText LockCrypt.Core:Strings:RevealField}" ShowText="False" Visibility="{Binding Meta,Converter={StaticResource MetaFlagsToVisibilityConverter},ConverterParameter=Password}"
                                                                     Command="loc:LockCryptCommands.ShowPassword" CommandParameter="{Binding Value}" CommandTarget="{Binding ElementName=txtValue}" />
                                        <l49ui:ImageButton Image="{StaticResource web_16}" Style="{StaticResource LiteImageButton}" ToolTip="{lex:LocText LockCrypt.Core:Strings:OpenUrl}" ShowText="False" Visibility="{Binding Meta,Converter={StaticResource MetaFlagsToVisibilityConverter},ConverterParameter=Url}"
                                                                     Command="loc:LockCryptCommands.OpenUrl" CommandParameter="{Binding Value}" />
                                        <Image x:Name="imgPhone" Visibility="{Binding Meta,Converter={StaticResource MetaFlagsToVisibilityConverter},ConverterParameter=Phone}" Source="{StaticResource phone_16}" Width="16" Stretch="None" VerticalAlignment="Center"/>
                                        <l49ui:ImageButton Image="{StaticResource copy_16}" Style="{StaticResource LiteImageButton}" ToolTip="{lex:LocText LockCrypt.Core:Strings:CopyToClipboard}" ShowText="False"
                                                                     Command="ApplicationCommands.Copy" CommandParameter="{Binding Value}" />
                                    </StackPanel>
                                </Grid>
                                <DataTemplate.Triggers>
                                    <DataTrigger Binding="{Binding Meta,Converter={StaticResource MetaFlagsToBoolConverter},ConverterParameter=Url}" Value="true">
                                        <Setter TargetName="txtValue" Property="Foreground" Value="{StaticResource hyperlinkPress}" />
                                        <Setter TargetName="txtValue" Property="TextDecorations" Value="Underline" />
                                    </DataTrigger>
                                </DataTemplate.Triggers>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>
                    <TextBlock Grid.Row="1" Margin="4,2,4,4" Text="{Binding Notes}" TextWrapping="Wrap" TextTrimming="CharacterEllipsis" Visibility="{Binding Template.ShowNotes,Converter={StaticResource BoolToVisibilityConverter}}" />
                </Grid>
            </ScrollViewer>
        </Border>
    </DataTemplate>
    <Style TargetType="{x:Type ListBox}" x:Key="WrappingListBox" BasedOn="{StaticResource {x:Type ListBox}}">
        <Style.Resources>
            <!--loc:DefaultDragSourceAdvisor x:Key="NestableDragSourceAdvisor"/>
            <loc:NestableDropTargetAdvisor x:Key="NestableDropTargetAdvisor"/-->
        </Style.Resources>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListBox}">
                    <Grid>
                        <Border BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" CornerRadius="2">
                            <ScrollViewer Margin="1" Focusable="false" Background="{TemplateBinding Background}" HorizontalScrollBarVisibility="Disabled">
                                <WrapPanel HorizontalAlignment="Left" VerticalAlignment="Top" IsItemsHost="True">
                                    <WrapPanel.Resources>
                                        <!--Style TargetType="ListBoxItem" BasedOn="{StaticResource {x:Type ListBoxItem}}">
                                            <Setter Property="loc:DragDropManager.DragSourceAdvisor" Value="{StaticResource NestableDragSourceAdvisor}"/>
                                            <Setter Property="loc:DragDropManager.DropTargetAdvisor" Value="{StaticResource NestableDropTargetAdvisor}"/>
                                        </Style-->
                                    </WrapPanel.Resources>
                                </WrapPanel>
                            </ScrollViewer>
                        </Border>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <ContextMenu x:Key="AccountViewContextMenu">
        <ContextMenu.Resources>
            <Image x:Key="copy_16" Source="pack://application:,,,/Resources/images/copy_16.png"/>
            <Image x:Key="edit_16" Source="pack://application:,,,/Resources/images/edit_16.png"/>
            <Image x:Key="delete_16" Source="pack://application:,,,/Resources/images/delete_16.png"/>
            <Image x:Key="keyboard_16" Source="pack://application:,,,/Resources/images/keyboard_16.png"/>
            <conv:NestableToVisibilityConverter x:Key="NestableToVisibilityConverter"/>
        </ContextMenu.Resources>
        <MenuItem Header="{lex:LocText LockCrypt.Core:Strings:Edit}" Command="ApplicationCommands.Properties" Icon="{StaticResource edit_16}"
                      CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget.SelectedItem}"/>
        <MenuItem Header="{lex:LocText LockCrypt.Core:Strings:Delete}" Command="ApplicationCommands.Delete" Icon="{StaticResource delete_16}" 
                      CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget.SelectedItem}"/>
        <MenuItem Header="{lex:LocText LockCrypt.Core:Strings:CreateCopy}" Command="loc:LockCryptCommands.CreateCopy" Icon="{StaticResource copy_16}" 
                  CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget.SelectedItem}" 
                  Visibility="{Binding PlacementTarget.SelectedItem,RelativeSource={RelativeSource AncestorType=ContextMenu},Converter={StaticResource NestableToVisibilityConverter},ConverterParameter=account}" />
        <MenuItem Header="{lex:LocText LockCrypt.Core:Strings:AutoType}" Command="loc:LockCryptCommands.AutoType" Icon="{StaticResource keyboard_16}"
                  CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget.SelectedItem}" 
                  Visibility="{Binding PlacementTarget.SelectedItem,RelativeSource={RelativeSource AncestorType=ContextMenu},Converter={StaticResource NestableToVisibilityConverter},ConverterParameter=account}" />
    </ContextMenu>
    <DataTemplate x:Key="GroupLargeIconsTemplate" DataType="{x:Type Core:IGroup}">
        <ListBox x:Name="itemContainer" ItemsSource="{Binding Children}" Style="{StaticResource WrappingListBox}" SelectionMode="Extended" ContextMenu="{StaticResource AccountViewContextMenu}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <DataTemplate.Resources>
                        <conv:IconConverter x:Key="IconConverter"/>
                    </DataTemplate.Resources>
                    <StackPanel Margin="2" Width="100" Height="140">
                        <Image Source="{Binding Icon,Converter={StaticResource IconConverter},ConverterParameter=96}" HorizontalAlignment="Center"/>
                        <TextBlock Text="{Binding Name}" TextWrapping="Wrap" TextTrimming="CharacterEllipsis" HorizontalAlignment="Center" TextAlignment="Center"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </DataTemplate>
    <DataTemplate x:Key="GroupSmallIconsTemplate" DataType="{x:Type Core:IGroup}">
        <ListBox x:Name="itemContainer" ItemsSource="{Binding Children}" Style="{StaticResource WrappingListBox}" SelectionMode="Extended" ContextMenu="{StaticResource AccountViewContextMenu}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <DataTemplate.Resources>
                        <conv:IconConverter x:Key="IconConverter"/>
                    </DataTemplate.Resources>
                    <StackPanel Margin="2" Width="68" Height="105">
                        <Image Source="{Binding Icon,Converter={StaticResource IconConverter},ConverterParameter=64}" HorizontalAlignment="Center"/>
                        <TextBlock Text="{Binding Name}" TextWrapping="Wrap" TextTrimming="CharacterEllipsis" TextAlignment="Center"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </DataTemplate>
    <DataTemplate x:Key="GroupTilesTemplate" DataType="{x:Type Core:IGroup}">
        <ListBox x:Name="itemContainer" ItemsSource="{Binding Children}" Style="{StaticResource WrappingListBox}" SelectionMode="Extended" ContextMenu="{StaticResource AccountViewContextMenu}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <DataTemplate.Resources>
                        <conv:IconConverter x:Key="IconConverter"/>
                    </DataTemplate.Resources>
                    <Grid HorizontalAlignment="Left" VerticalAlignment="Center" Height="40" Width="200">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="32"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <Image Grid.Column="0" Source="{Binding Icon,Converter={StaticResource IconConverter},ConverterParameter=32}" VerticalAlignment="Center"/>
                        <TextBlock Grid.Column="1" Text="{Binding Name}" TextWrapping="Wrap" TextTrimming="CharacterEllipsis" VerticalAlignment="Center" />
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </DataTemplate>
    <DataTemplate x:Key="GroupDetailsTemplate" DataType="{x:Type Core:IGroup}">
        <DataTemplate.Resources>
            <conv:IconConverter x:Key="IconConverter"/>
            <conv:TemplateNameSanitizer x:Key="TemplateNameSanitizer"/>
        </DataTemplate.Resources>
        <dg:DataGrid x:Name="itemContainer" ItemsSource="{Binding Children}" AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="False" CanUserSortColumns="True"
                     VerticalAlignment="Stretch" SelectionMode="Single" SelectionUnit="FullRow" ContextMenu="{StaticResource AccountViewContextMenu}"
                     HeadersVisibility="Column" EnableRowVirtualization="True" GridLinesVisibility="Horizontal">
            <dg:DataGrid.Columns>
                <dg:DataGridTemplateColumn Width="26">
                    <dg:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Image Source="{Binding Icon,Converter={StaticResource IconConverter},ConverterParameter=16}" Width="16" Height="16" HorizontalAlignment="Center"/>
                        </DataTemplate>
                    </dg:DataGridTemplateColumn.CellTemplate>
                </dg:DataGridTemplateColumn>
                <dg:DataGridTemplateColumn Width="300" Header="{lex:LocText LockCrypt.Core:Strings:Name}">
                    <dg:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Name}" TextWrapping="Wrap" TextTrimming="CharacterEllipsis"/>
                        </DataTemplate>
                    </dg:DataGridTemplateColumn.CellTemplate>
                </dg:DataGridTemplateColumn>
                <dg:DataGridTemplateColumn Header="{lex:LocText LockCrypt.Core:Strings:Type}">
                    <dg:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Converter={StaticResource TemplateNameSanitizer}}" TextWrapping="Wrap" TextTrimming="CharacterEllipsis"/>
                        </DataTemplate>
                    </dg:DataGridTemplateColumn.CellTemplate>
                </dg:DataGridTemplateColumn>
                <dg:DataGridTemplateColumn Width="130" Header="{lex:LocText LockCrypt.Core:Strings:Created}">
                    <dg:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Created,StringFormat=D}" TextWrapping="Wrap" TextTrimming="CharacterEllipsis"/>
                        </DataTemplate>
                    </dg:DataGridTemplateColumn.CellTemplate>
                </dg:DataGridTemplateColumn>
            </dg:DataGrid.Columns>
        </dg:DataGrid>
    </DataTemplate>

</ResourceDictionary>